package com.zcl.system.test;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zcl.system.entity.Student;
import com.zcl.system.service.IStudentService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

@SpringBootTest
public class test {
    @Autowired
    private IStudentService iStudentService;
    //查询所有学生
    @Test
    public void test01(){
       List<Student> students= iStudentService.list();
        for (Student student : students) {
            System.out.println("student = " + student);
        }
    }
    //分页查询学生
    @Test
    public void test02(){
        //定义分页参数
        //当前页
        long page=2;
        //每页显示的数量
        long pageSize=5;
//        开始分页
        Page<Student> studentPage =iStudentService.page(new Page<>(page, pageSize));
        //打印分页结果
        System.out.println("studentPage = " + studentPage);
    }
    //分页带条件查询
    @Test
    public void test03(){
        //定义分页参数
        //当前页
        long page=1;
        //每页显示的数量
        long pageSize=5;
        //包装查询条件的对象
        QueryWrapper<Student> wrapper = new QueryWrapper<>();
        //添加,包装查询条件,like模糊查询(字段名称,包含的内容)
        wrapper.like("sname","爆");
        //开始分页查询
        Page<Student> studentPage = iStudentService.page(new Page<>(page, pageSize), wrapper);
        //打印结果
        System.out.println("studentPage = " + studentPage);
    }
    //根据学生ID查询学生
    @Test
    public void test04(){
         Student student = iStudentService.getById(3);
        System.out.println("student = " + student);
    }
    //添加学生
    @Test
    public void test05(){
        Student student = new Student("核爆酱肘子",2002,2,23,"女","成都");
        iStudentService.save(student);
    }
    //修改学生
    @Test
    public void test07(){
        //根据学生id查出学生
        Student student = iStudentService.getById(2);
        //修改学生信息
        student.setSname("避雷塔");
        //开始修改操作
        iStudentService.updateById(student);
    }
    //删除学生
    @Test
    public void test06(){
        //根据ID删除学生,真正意义上的从数据库中删除
        //一般情况下会在数据库加上delete的字段,设置属性,仅在日后在查询结果中过滤掉
        iStudentService.removeById(222222245);
    }
}
